/**
 * @Date: Mar 8, 2010 8:07:06 PM
 */
package com.philip.journal.home.controller.action;

import java.io.IOException;

import javax.servlet.ServletException;

import com.philip.journal.core.controller.ResponseHandler;
import com.philip.journal.core.service.ServiceProxy;

/**
 * For moving Branch or Entry in the tree.
 *
 * @author cry30
 */
public class MoveNodeAction extends AbstractEntryAction {

    /**
     * Default delegate constructor.
     *
     * @param serviceProxy Service proxy to be injected by Spring.
     * @param responseHandler Response handler to be injected by Spring.
     */
    public MoveNodeAction(final ServiceProxy serviceProxy, final ResponseHandler responseHandler) {
        super(serviceProxy, responseHandler);
    }

    @Override
    public void execute() throws ServletException, IOException
    {
        final String parent = getHttpServletRequest().getParameter(PARENT_ID);
        final String node = getHttpServletRequest().getParameter(NODE_ID);
        final String leaf = getHttpServletRequest().getParameter(IS_LEAF);

        final boolean isLeaf = Boolean.parseBoolean(leaf);
        final long nodeId = parseId(node);
        final long parentId = parseId(parent);
        if (isLeaf) {
            getServiceProxy().moveEntry(parentId, nodeId);
        } else {
            getServiceProxy().moveBranch(parentId, nodeId);
        }
        getResponseHandler().respondSuccess(getHttpServletResponse());
    }
}
